package kizzo.game.tile_objects 
{
	import away3d.entities.Mesh;
	import away3d.events.Object3DEvent;
	import away3d.lights.LightBase;
	import away3d.materials.TextureMaterial;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Vector3D;
	import kizzo.game.TileState;
	import kizzo.geometry.TileCoord3D;
	/**
	 * ...
	 * @author kizzo
	 */
	public class TileObject extends Sprite
	{		
		
		public var mesh:Mesh;
		public var tile_coord:TileCoord3D;
		public var type:uint;
		
		private var _is_tile:Boolean;
		public var is_active:Boolean;
		
		protected var light:LightBase;
		
		protected var tween_alpha_step:int;
		protected var tween_alpha_start:Number;
		protected var tween_alpha_target:Number;
		protected var TWEEN_ALPHA_STEP_NUMBER:int = 5;	
		public static var TWEEN_ALPHA_COMPLETE:String = "TWEEN_ALPHA_COMPLETE";
				
		public function TileObject(mesh:Mesh, tile_coord:TileCoord3D, type:uint, light:LightBase = null) 
		{
			this.mesh = mesh;
			this.tile_coord = tile_coord;
			this.type = type;			
			
			this.light = light;
			this.is_active = false;
			
			if (light!= null)
				mesh.addEventListener(Object3DEvent.POSITION_CHANGED, pos_change_hnd);
			
			_is_tile = false;
			if (   	this.type == TileState.NEGATOR_TILE ||
					this.type == TileState.BOOST_MOD ||
					this.type == TileState.ROTATE_CLOCKWISE_MOD ||
					this.type == TileState.SIMPLE_TILE ||
					this.type == TileState.SLAVE_LINK_A ||
					this.type == TileState.SLAVE_GOAL_A ||
					this.type == TileState.CRUMBLING_TILE ||					
					this.type == TileState.MASTER_GOAL ||
					this.type == TileState.TELEPORT_TILE ||
					this.type == TileState.MIRROR_Z ||
					this.type == TileState.MIRROR_X ||
					this.type == TileState.ACTIVATED ||
					this.type == TileState.ACTIVATOR)	
				_is_tile = true;	
		}
		
		
		public function is_tile():Boolean
		{
			return _is_tile;
		}
		
		
		public function move_to(tc:TileCoord3D):void
		{
			this.tile_coord.x = tc.x;
			this.tile_coord.z = tc.z;
			this.mesh.position = new Vector3D(tc.x_m, mesh.position.y, tc.z_m);
		}
		
		
		private function pos_change_hnd(e:Event):void
		{
			light.x = mesh.x;
			light.y = mesh.y;
			light.z = mesh.z;
		}
		
		public function tween_alpha(to_value:Number, TWEEN_ALPHA_STEP_NUMBER:int=5):void
		{
			TWEEN_ALPHA_STEP_NUMBER = TWEEN_ALPHA_STEP_NUMBER;
			tween_alpha_start =  (this.mesh.material as TextureMaterial).alpha;
			tween_alpha_target = to_value;
			tween_alpha_step = TWEEN_ALPHA_STEP_NUMBER;
			addEventListener(Event.ENTER_FRAME, tween_alpha_hnd);
		}
		
		
		protected function tween_alpha_hnd(e:Event):void
		{			
			tween_alpha_step--;
						
			(this.mesh.material as TextureMaterial).alpha = tween_alpha_start + (tween_alpha_target-tween_alpha_start)*(1-tween_alpha_step / TWEEN_ALPHA_STEP_NUMBER);
			
			if (tween_alpha_step == 0)
			{
				removeEventListener(Event.ENTER_FRAME, tween_alpha_hnd);
				dispatchEvent(new Event(TWEEN_ALPHA_COMPLETE));
			}
		}		
		
	}

}